home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / RadarScope-c / RadarScope.c next >
Encoding:
Text File  |  1994-12-04  |  7.3 KB  |  385 lines  |  [TEXT/KAHL]

  1. //• RadarScope.c
  2.  
  3.  
  4. #include <math.h>
  5.  
  6. #define over qd.screenBits.bounds.right
  7. #define down qd.screenBits.bounds.bottom
  8.  
  9. #define     drawWindow 2
  10.  
  11. typedef struct Info
  12. {
  13.     RgnHandle clip;
  14.     Rect rect;
  15.     short h;
  16.     short v;
  17. } Info;
  18.  
  19. Info stuff[20];
  20.  
  21. typedef struct
  22. {
  23.     WindowRecord    windRec;
  24.     short             windtype;
  25.     Boolean         drawnIn;
  26. } MyWindRecord, *MyWindPeek;
  27.  
  28. MenuHandle appleMenu, fileMenu, editMenu, widthMenu;
  29.  
  30. enum {
  31.     appleID = 1,
  32.     fileID,
  33.     editID,
  34.     widthID
  35. };
  36. enum {
  37.     openItem = 1,
  38.     closeItem,
  39.     quitItem = 4
  40. };
  41. Point centre, circle[360], deltaPt, lastPt, whereLast;
  42.  
  43. short     bottom, lasti, radius, width, height, right, left, bottom, top;
  44.  
  45. Rect globWind, ourRect;
  46. EventRecord theEvent;                
  47. RgnHandle tempRgnHandle;
  48. Str255 strangs[20];
  49. WindowPtr shell_window;
  50. short        windowType;             
  51.  
  52. void InitRadar (Rect *drawArea, short ID);
  53. void DrawRadar (short ID);
  54. void Action (void);
  55. void SetUp (short ID);
  56. void TakeDown (short ID);
  57. void StowInfo (Rect *tempRect, short ID);
  58. void SetUpWindow (void);
  59. Boolean DetermineDrawnInFlag (WindowPtr theWindow);
  60. void UpdateDrawWindow (WindowPtr theWindow);
  61. void DoSomeUpdateStuff (void);
  62. void HandleUpdate (void);
  63. short DetermineWindowType (WindowPtr theWindow);
  64. void InitManagers (short numMasters);
  65. short MainEvent (void);
  66. void DoAbout (void);
  67. void SetUpMenus (void);
  68. void DoKeyDownStuff (void);
  69. short HandleMenu (long mSelect);
  70. void main (void);
  71.  
  72. void InitRadar (Rect *drawArea, short ID)
  73. {
  74.     short i;
  75.     double sinner, cosinner, thetaStep, theta;
  76.     
  77.     //• Our rectangle is at the drawArea, which is tempRect, which
  78.     //• is the one 4 pixels in, in the window.
  79.     ourRect = *drawArea;
  80.     
  81.     //• So, width is 192 pixels, and so is height.
  82.     width = ourRect.right;
  83.     height = ourRect.bottom;
  84.     
  85.     //• This makes radius half that, or 96
  86.     radius = width / 2;
  87.     centre.h = radius;
  88.     centre.v = radius;
  89.     thetaStep = (2 * 3.14159) / 360;
  90.     theta = 0;
  91.     for (i = 0; i < 360; i++)
  92.     {
  93.         circle[i].h = radius * cos (theta) + radius;
  94.         circle[i].v = radius * sin (theta) + radius;
  95.         theta += thetaStep;
  96.     }
  97.     lasti = 0;
  98. }
  99.  
  100. void DrawRadar (short ID)
  101. {
  102.     FillOval (&ourRect, black);
  103.     MoveTo (centre.h, centre.v);
  104.     PenMode (srcXor);
  105.     LineTo (circle[lasti].h, circle[lasti].v);
  106.     lasti = (++lasti % 360);
  107.     PenPat (black);
  108. }
  109.  
  110. void Action (void)
  111. {
  112.     SetUp (1);
  113.     DrawRadar (1);
  114.     TakeDown (1);
  115. }
  116.  
  117. void SetUp (short ID)
  118. {
  119.     SetClip (stuff[ID].clip);
  120.     SetOrigin (stuff[ID].h, stuff[ID].v);
  121. }
  122.  
  123. void TakeDown (short ID)
  124. {
  125.     SetOrigin (0, 0);
  126. }
  127.  
  128. void StowInfo (Rect *tempRect, short ID)
  129. {
  130.     RgnHandle tempRgn;
  131.     
  132.     stuff[ID].rect = *tempRect;
  133.     stuff[ID].h = - tempRect->left;
  134.     stuff[ID].v = - tempRect->top;
  135.     
  136.     InsetRect (&stuff[ID].rect, -1, -1);
  137.     tempRgn = NewRgn ();
  138.     OffsetRect (tempRect, stuff[ID].h, stuff[ID].v);
  139.     RectRgn (tempRgn, tempRect);
  140.     stuff[ID].clip = tempRgn;
  141. }
  142.  
  143. void SetUpWindow (void)
  144. {
  145.     Rect tempRect, frameRect, ovalRect;
  146.     RgnHandle tempRgn;
  147.     
  148.     //• Since QuickDraw has the port set globally (default),
  149.     //• we get the screen size off right and bottom, and
  150.     //• backtrack off it. The below results in a 200x200
  151.     //• window centered on the screen.
  152.     SetRect (&globWind, over / 2 - 100,
  153.                         down / 2 - 100,
  154.                         over / 2 + 100,
  155.                         down / 2 + 100);
  156.                         
  157.     shell_window = NewWindow (0L, &globWind, "\pKen's Shell", true, 
  158.                             plainDBox, (WindowPtr) -1L, true, 0);
  159.  
  160.     //• Now we've said the WindowPtr points to a window in the
  161.     //• rectangle we set off the screen size. So we localize our
  162.     //• coordinates to that window by saying IT is the port that
  163.     //• QuickDraw will draw in.
  164.     SetPort (shell_window);
  165.     
  166.     //• Now we set a rectangle inside that local port. Since it's
  167.     //• a 200 pixel square window, it's 2 pixels in on all sides.
  168.     SetRect (&frameRect, 2, 2, 198, 198);
  169.     
  170.     //• Now we frame it.
  171.     FrameRect (&frameRect);
  172.  
  173.     //• Now we set another one 4 pixels in, to show our stuff in.
  174.     SetRect (&tempRect, 4, 4, 196, 196);
  175.     
  176.     StowInfo (&tempRect, 1);
  177.     InitRadar (&tempRect, 1);
  178.     
  179.     stuff[0].clip = NewRgn ();
  180.     GetClip (stuff[0].clip);
  181. }
  182.  
  183. void DoSomeUpdateStuff (void)
  184. {
  185.     short i;
  186.     SetClip (stuff[0].clip);
  187.     FrameRect (&stuff[i].rect);
  188. }
  189.  
  190. void InitManagers (short numMasters)
  191. {
  192.     short i;
  193.     InitGraf (&thePort);
  194.     InitFonts ();
  195.     InitWindows ();
  196.     InitMenus ();
  197.     TEInit ();
  198.     InitDialogs (((void *) 0));
  199.     for (i = 0; i < 5; i++)
  200.     {
  201.         MoreMasters ();
  202.     }
  203.     FlushEvents (everyEvent, 0);
  204.     InitCursor ();
  205. }
  206.  
  207. short MainEvent ()
  208. {
  209.     WindowPtr whichWindow;
  210.     Rect r;
  211.     SystemTask ();
  212.     if (GetNextEvent (everyEvent, &theEvent))
  213.     {
  214.         switch (theEvent.what)
  215.         {
  216.             case mouseDown:
  217.             switch (FindWindow (theEvent.where, &whichWindow))
  218.             {
  219.                 case inDesk:
  220.                 break;
  221.                 
  222.                 case inMenuBar:
  223.                     return (HandleMenu (MenuSelect (theEvent.where)));
  224.                 break;
  225.                 
  226.                 case inGoAway:
  227.                 break;
  228.                 
  229.                 case inSysWindow:
  230.                 break;
  231.                 
  232.                 case inContent:
  233.                 break;
  234.                 
  235.                 case inDrag:
  236.                     SetRect (&r, 0, 0, 5000, 5000);
  237.                     DragWindow (whichWindow, theEvent.where, &r);
  238.                 break;
  239.             }
  240.             break;
  241.             
  242.             case keyDown:
  243.             case autoKey:
  244.                 DoKeyDownStuff ();
  245.             break;
  246.             
  247.             case activateEvt:
  248.             break;
  249.             
  250.             case updateEvt:
  251.             BeginUpdate (shell_window);
  252. //            HandleUpdate ();                     
  253.             DoSomeUpdateStuff ();
  254.             EndUpdate (shell_window);
  255.             break;
  256.             
  257.             default:;
  258.         }
  259.     }
  260.     Action ();
  261.     HiliteMenu (0);
  262.     return (1);
  263. }
  264.  
  265. void DoAbout ()
  266. {
  267.     Boolean done;
  268.     short theItem;
  269.     DialogPtr AboutPtr;
  270.     short itemType;
  271.     Handle item;
  272.     Rect box;
  273.     done = 0;
  274.     AboutPtr = GetNewDialog (128,0L, (WindowPtr)-1);
  275.     while (!done)
  276.     {
  277.         ModalDialog (0L,&theItem);
  278.         switch (theItem)
  279.         {
  280.             case OK:
  281.             done = 1;
  282.             break;
  283.             
  284.             case Cancel:
  285.             done = 1;
  286.             break;
  287.         }
  288.     }
  289.     DisposDialog (AboutPtr);
  290. }
  291.  
  292. void SetUpMenus (void)
  293. {
  294.     InsertMenu (appleMenu = NewMenu (appleID, "\p\024"), 0);
  295.     InsertMenu (fileMenu = NewMenu (fileID, "\pFile"), 0);
  296.     InsertMenu (editMenu = NewMenu (editID, "\pEdit"), 0);
  297.     InsertMenu (widthMenu = NewMenu (widthID, "\pBlips"), 0);
  298.     DrawMenuBar ();
  299.     AddResMenu (appleMenu, 'DRVR');
  300.     AppendMenu (fileMenu, "\pOpen/O;Close/W; (-;Quit/Q");
  301.     AppendMenu (editMenu, "\pUndo/Z; (-;Cut/X;Copy/C;Paste/V;Clear");
  302.     AppendMenu (widthMenu, "\p0/0;1/1;2/2;3/3;4/4;5/5;6/6;7/7;8/8;9/9");
  303. }
  304.  
  305. void DoKeyDownStuff ()
  306. {
  307.     short chr; 
  308.     long  menuChoice;
  309.   
  310.     chr = theEvent.message & charCodeMask;
  311.   
  312.     if ((theEvent.modifiers & cmdKey) != 0)
  313.     {
  314.         if (theEvent.what != autoKey) 
  315.         {
  316.             menuChoice = MenuKey (chr);
  317.             HandleMenu (menuChoice);
  318.         }
  319.     }
  320. }
  321.  
  322. short HandleMenu (long mSelect)
  323. {
  324.     short menuID = HiWord (mSelect);
  325.     short menuItem = LoWord (mSelect);
  326.     Str255 name;
  327.     GrafPtr savePort;
  328.     WindowPeek frontWindow;
  329.     switch (menuID)
  330.     {
  331.         case appleID:
  332.             GetPort (&savePort);
  333.             GetItem (appleMenu, menuItem, name);
  334.             OpenDeskAcc (name);
  335.             SetPort (savePort);
  336.         break;
  337.         
  338.         case fileID:
  339.             switch (menuItem)
  340.             {
  341.                 case openItem:
  342.                     ShowWindow (shell_window);
  343.                     SelectWindow (shell_window);
  344.                 break;
  345.                 
  346.                 case closeItem:
  347.                     if ((frontWindow = (WindowPeek) FrontWindow ()) == 0L)
  348.                         break;
  349.                 
  350.                     if (frontWindow->windowKind < 0)
  351.                         CloseDeskAcc (frontWindow->windowKind);
  352.                     else 
  353.                         if (frontWindow = (WindowPeek) shell_window)
  354.                             HideWindow (shell_window);
  355.                 break;
  356.                 
  357.                 case quitItem:
  358.                     ExitToShell ();
  359.                 break;
  360.             }
  361.         break;
  362.         
  363.         case editID:
  364.             if (!SystemEdit (menuItem-1))
  365.                 SysBeep (5);
  366.         break;
  367.         
  368.         case widthID:
  369.             CheckItem (widthMenu, width, false);
  370.             width = menuItem;
  371.             InvalRect (&shell_window->portRect);
  372.         break;
  373.     }
  374. }
  375.  
  376. void main ()
  377. {
  378.     InitManagers (5);
  379.     SetUpMenus ();
  380.     SetUpWindow ();
  381.     for (;;)
  382.         MainEvent ();
  383. }
  384.  
  385.